package com.mobile.ado.setting.ringtone;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;

import com.mobile.ado.R;

public class RingtoneSettingActivity extends PreferenceActivity {

	private Activity activity;
	private CheckBoxPreference ringtoneEnabler;
	private PreferenceScreen timerPreferenceScreen;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.ringtone_preferences);

		activity = this;

		setupRingtoneEnablerCheckBoxPreference();
		setupTimerPreferenceScreen();

	}

	private void setupTimerPreferenceScreen() {
		timerPreferenceScreen = (PreferenceScreen) findPreference(getString(R.string.pref_ringtone_time_key));
		timerPreferenceScreen
				.setOnPreferenceClickListener(new OnPreferenceClickListener() {

					@Override
					public boolean onPreferenceClick(Preference preference) {
						Intent intent = new Intent(activity,
								RingtoneSettingTimerActivity.class);
						activity.startActivity(intent);

						return false;
					}
				});
	}

	private void setupRingtoneEnablerCheckBoxPreference() {
		ringtoneEnabler = (CheckBoxPreference) findPreference(getString(R.string.pref_ringtone_onoff_key));

		if (ringtoneEnabler.isChecked()) {
			startScheduler();
		} else {
			stopSchedule();
		}
		ringtoneEnabler
				.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

					@Override
					public boolean onPreferenceChange(Preference preference,
							Object newValue) {
						if (newValue == Boolean.FALSE) {
							stopSchedule();
						} else if (newValue == Boolean.TRUE) {
							startScheduler();
						}
						return true;
					}
				});
	}

	public void startScheduler() {

		Intent serviceIntent = new Intent(this, RingtoneSettingService.class);
		serviceIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		startService(serviceIntent);
	}

	private void stopSchedule() {
		Intent intent = new Intent(this, RingtoneSettingService.class);
		stopService(intent);
	}

	private void startBootReceiver() {
		ComponentName receiver = new ComponentName(this, BootReceiver.class);
		PackageManager pm = this.getPackageManager();
		pm.setComponentEnabledSetting(receiver,
				PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
				PackageManager.DONT_KILL_APP);
	}

	private void stopBootReceiver() {
		ComponentName receiver = new ComponentName(this, BootReceiver.class);
		PackageManager pm = this.getPackageManager();
		pm.setComponentEnabledSetting(receiver,
				PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
				PackageManager.DONT_KILL_APP);
	}

}
